<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class LoginUserApiController extends AbstractRestfulController
{
    public function getList()
    {
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');		
		$getUserlist = $userTable->getUserlist();	
		if(($getUserlist->count())!=0){
		$data = array();$i=0;
				foreach($getUserlist as $users){
					$uid=$users->user_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['user_name']= $users->user_name;
					$data[$i]['user_email']= $users->user_email;
					$data[$i]['user_mobile']= $users->user_mobile;
					$data[$i]['action']='<a href="javascript:void(0)" onclick = "editUserFunction('.$uid.',0)">Edit</a> &nbsp;/&nbsp;<a href="javascript:void(0);" onClick="deleteuser('.$uid.')">Delete</a>';
					$i++;
				}
				$data['aaData'] = $data;
				echo json_encode($data['aaData']); exit;
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	 =>	1,
			));
		}	
    }
    public function get($email)
    {
		header('Access-Control-Allow-Origin: *');		
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');		
		$checkUser = $userTable->checkUniqueRecord($email)->toarray();		
		if(count($checkUser)!=0){
			return new JsonModel(array(
				'data' => 'Email already exists',
				'value' => 1,
			));			
		}else{
			return new JsonModel(array(
				'value' => 0,
				'data'  => 'valid email',
			));			
		}
    }
    public function create($userdata)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');
		$checkUserloginemail = $userTable->checkloginEmail($userdata)->toArray();
			if(count($checkUserloginemail)!=0){
				return new JsonModel(array(
					'status'		=>	'success',
					'userData'		=>	$checkUserloginemail[0]['user_id'],
					'value'			=>	1,
				));	
			}else{
				return new JsonModel(array(
					'status'	=>	'fail',
					'value'		=>   0,
				));			
			}
	}
    public function update($id, $updatedata)
    {
        
    }
    public function delete($userid)
    {
      $userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');
		$deleteUser = $userTable->deleteUser($userid);
			if($deleteUser!=0){
				return new JsonModel(array(
					'status'		=>	'success',
					'value'			=>	1,
				));	
			}else{
				return new JsonModel(array(
					'status'	=>	'fail',
					'value'		=>   0,
				));			
			}
    }
}